{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [\n",
    "    [19, 50, 10, 37, 82, 31],\n",
    "    [25, 61, 39, 88, 43, 57],\n",
    "    [10, 10, 28, 32, 45, 92],\n",
    "    [33, 21, 87, 10, 54, 48],\n",
    "    [10, 10, 44, 66, 83, 32],\n",
    "    [75, 53, 49, 10, 11, 92]\n",
    "    ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这里map函数仅考虑周围有8个值的点\n",
    "def my_map(nums):\n",
    "    result_path = {}\n",
    "    each_path = {}\n",
    "    row = len(nums)\n",
    "    col = len(nums[0])\n",
    "    for i in range(row):\n",
    "        for k in range(col):\n",
    "            each_path[(i+1, k+1)] = nums[i][k]\n",
    "    for j in range(1,row-1):\n",
    "        for h in range(1,col-1):\n",
    "            result_path[(j+1, h+1)] = [\n",
    "                each_path[(j, h)],\n",
    "                each_path[(j, h+1)],\n",
    "                each_path[(j, h+2)],\n",
    "                each_path[(j+1, h)],\n",
    "                each_path[(j+1, h+2)],\n",
    "                each_path[(j+2, h)],\n",
    "                each_path[(j+2, h+1)],\n",
    "                each_path[(j+2, h+2)]\n",
    "                ]\n",
    "    return result_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(2, 2): [19, 50, 10, 25, 39, 10, 10, 28],\n",
       " (2, 3): [50, 10, 37, 61, 88, 10, 28, 32],\n",
       " (2, 4): [10, 37, 82, 39, 43, 28, 32, 45],\n",
       " (2, 5): [37, 82, 31, 88, 57, 32, 45, 92],\n",
       " (3, 2): [25, 61, 39, 10, 28, 33, 21, 87],\n",
       " (3, 3): [61, 39, 88, 10, 32, 21, 87, 10],\n",
       " (3, 4): [39, 88, 43, 28, 45, 87, 10, 54],\n",
       " (3, 5): [88, 43, 57, 32, 92, 10, 54, 48],\n",
       " (4, 2): [10, 10, 28, 33, 87, 10, 10, 44],\n",
       " (4, 3): [10, 28, 32, 21, 10, 10, 44, 66],\n",
       " (4, 4): [28, 32, 45, 87, 54, 44, 66, 83],\n",
       " (4, 5): [32, 45, 92, 10, 48, 66, 83, 32],\n",
       " (5, 2): [33, 21, 87, 10, 44, 75, 53, 49],\n",
       " (5, 3): [21, 87, 10, 10, 66, 53, 49, 10],\n",
       " (5, 4): [87, 10, 54, 44, 83, 49, 10, 11],\n",
       " (5, 5): [10, 54, 48, 66, 32, 10, 11, 92]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = my_map(nums)\n",
    "path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_reduce(tuple,path):\n",
    "    sum = 0\n",
    "    for i in path[tuple]:\n",
    "        sum = sum + i\n",
    "    return '{} --> {}'.format(tuple, sum/8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4) --> 49.25\n"
     ]
    }
   ],
   "source": [
    "print(my_reduce((3, 4), path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
